/*
 * Logger.h
 *
 *  Created on: Oct 9, 2014
 *      Authors: Avihay Avitan & Stefan Farcas
 */
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include "AllUtils.h"
#include "../CoreSettings/CoreSettings.h"
#include "../ProgressStatusPublisher/ProgressStatusPublisher.h"

using namespace std ;
using namespace SettingsSpace;
using namespace ProgressStatusSpace;

#ifndef LOGGER_H_
#define LOGGER_H_

namespace LoggerNameSpace {

/**
 * LoggingColor is an enum class defines the color that can be printed to terminal
 */
enum class LoggingColor { BLACK = 0, BLUE = 1 , MAGENTA = 2, RED = 3, GREEN = 4 , CYAN = 5 };

/**
 *  Logger is a class that implements logging functionality to log file and/or terminal
 *  see [Logger] section in core.ini file
 */
class Logger {


public:
	/**
	 * implementing singleton pattern of the main logging file - GSPASS.log
	 * returning pointer to logger instance
	 */
    static Logger* & getGSLogger()
    {
        static Logger *   instance = new Logger(CoreSettings::getInstance().getGSLogFilePath());; // Guaranteed to be destroyed.
                              // Instantiated on first use.
        return instance;
    }
	Logger (string filename);
	virtual ~Logger () ;
	void logInfo (ProgressStatus status);
	void logInfo (ProgressStatus status, LoggingColor color);
	void logError (ProgressStatus status);
	void logWarning (ProgressStatus status);
	void logInfoCR(ProgressStatus status, LoggingColor color, bool isFirst);
	void publish(ProgressStatus status);

private:
	FILE * logfile;
	ProgressStatusPublisher * publisher;
	timespec timespc;
	bool isCR;
	bool CRfirst;
	void writeLineTofile (string text);
	void writeLineToTerminal (string text);
	void log (ProgressStatus status, LoggingColor color);
	string fillTextWithColor(string text, LoggingColor color);

} ;

//static Logger * gsLogger;

} /* namespace LoggerNameSpace */

#endif /* LOGGER_H_ */
